//
//  ViewController.m
//  OatosMacDemo
//
//  Created by 李少游 on 2017/3/24.
//
//

#import "ViewController.h"
#import "Masonry.h"

@interface ViewController()
{
    NSMutableArray * _dataArray;
}

@end


@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [NSFileManager defaultManager];
    _dataArray = [NSMutableArray array];
    
}
- (IBAction)doubleClickTableRow:(id)sender {
    NSTableView *tabelView = sender;
    NSInteger row =  [tabelView clickedRow];
    if (row == -1) {
        return;
    }
    
    
    
    
    
     NSURL *fileUrl = _dataArray[row];
//    NSArray *fileInfo = [file componentsSeparatedByString:@"."];
//    NSString *fileName = [fileInfo firstObject];
//    NSString *path= [[NSBundle mainBundle] pathForResource:fileName ofType:[fileInfo lastObject]];
//    NSURL *fileUrl = [NSURL fileURLWithPath:path];
    
    NSLog(@"fileUrl = %@",fileUrl);
    
    [[NSWorkspace sharedWorkspace] openURL:fileUrl];
}
- (IBAction)addFile:(id)sender {
    
    NSOpenPanel *panel = [NSOpenPanel openPanel];
//    [panel setDirectory:NSHomeDirectoryForUser(nil)]; // Set panel's default directory.
    [panel setAllowedFileTypes:nil]; // Set what kind of file to select.
    [panel setAllowsMultipleSelection:YES];
    // More panel configure code.
    [panel beginSheetModalForWindow:[NSApplication sharedApplication].keyWindow completionHandler: (^(NSInteger result){
        if(result == NSOKButton) {
            NSArray *fileURLs = [panel URLs];
            NSLog(@"%@", fileURLs);
            [_dataArray addObjectsFromArray:fileURLs];
            [self.myTableView reloadData];
        }
    })];
 
    
}
#pragma mark-NSTableViewDataSource
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{
    return _dataArray.count;
}

- (nullable NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(nullable NSTableColumn *)tableColumn row:(NSInteger)row NS_AVAILABLE_MAC(10_7){
    NSTableCellView *cell = [tableView makeViewWithIdentifier:@"mycell" owner:self];
    NSURL *fileUrl = _dataArray[row];
    cell.textField.stringValue = [fileUrl lastPathComponent];;
    return cell;
}

#pragma mark - NSTableViewDelegate
- (BOOL)tableView:(NSTableView *)tableView shouldSelectRow:(NSInteger)row{
    return YES;
}
- (void)tableView:(NSTableView *)tableView didClickTableColumn:(NSTableColumn *)tableColumn{
    NSLog(@"didClickTableColumn");
}
- (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row{
    return  50;
}

@end
